package com.zfzs.post.kuaidi;

import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.http.HttpUtils;
import com.zfzs.post.utils.JsonUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: ck
 * @create: 2019-12-11 15:15
 **/
public class KuaiDiUtils {
    public static String getKuaiDiInfo(String kuaiDiNo, String type) {
        String host = "https://wuliu.market.alicloudapi.com";
        String path = "/kdi";
        String method = "GET";
        String appcode = "def7ce1e89e74afaa8f844ab82f3c955";  // !!!替换填写自己的AppCode 在买家中心查看
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode); //格式为:Authorization:APPCODE 83359fd73fe11248385f570e3c139xxx
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("no", kuaiDiNo);// !!! 请求参数
        querys.put("type", type);// !!! 请求参数
        String result = "";
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //System.out.println(response.toString());//如不输出json, 请打开这行代码，打印调试头部状态码。
            //状态码: 200 正常；400 URL无效；401 appCode错误； 403 次数用完； 500 API网管错误
            //获取response的body
            String body = EntityUtils.toString(response.getEntity()); //输出json
            Map<String, Object> map = JsonUtils.fromJson(body);
            String status = map.get("status").toString();
            if (!"0".equals(status)) {
                throw new BadRequestException("未查询到物流信息");
            }
            result = map.get("result").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public static void main(String[] args) {
        String res = getKuaiDiInfo("368914830202", null);
        System.out.println(res);
    }
}
